/*
 * Copyright (c) 2024, Qi Yaolong.
 * dragon@hbu.edu.cn
 * HeBei University, China.
 * 
 * 简体中文
 * 保留所有权利。除非符合（遵守）以下条款，否则您不得使用此文件：
 * 
 * 禁止任何未经授权的个人或组织以任何形式将本程序及其附属资料、创新、创意、架构设计、算法、衍生作品
 * 等用于任何商业目的、盈利活动、各类竞赛(比赛)、直播教学、录播教学、线下课程、书籍编写、教材编写、
 * 会议、培训、公益活动、项目课题、毕业设计、毕业论文、学术论文等。
 * 
 * 本程序由版权持有人和贡献人“按原样”提供，不提供任何明示或暗示担保，包括但不局限于对适销性和特定
 * 用途适合性的暗示担保。在任何情况下，版权持有人或贡献人对因使用本程序而导致的任何直接、间接、附带、
 * 特殊、典型或后果性损害（包括但不限于购买替代商品或服务；使用损失、数据丢失或利润损失；业务中断）
 * 不承担任何责任，无论其如何产生，也不论其责任理论为何，无论是合同、严格责任还是侵权（包括疏忽或
 * 其他），即使已告知此类损害的可能性。
 * 
 * English
 * All rights reserved. You may NOT use this file except in compliance with the
 * following terms:
 * 
 * IT IS PROHIBITED FOR ANY UNAUTHORIZED PERSON OR ENTITY TO USE THIS PROGRAM AND 
 * ITS ANCILLARY DATA, INNOVATIONS, CONCEPTS, ARCHITECTURE DESIGN, ALGORITHMS, 
 * DERIVED WORKS, ETC., IN ANY FORM FOR COMMERCIAL PURPOSES, PROFIT-MAKING 
 * ACTIVITIES, VARIOUS COMPETITIONS (CONTESTS), LIVE TEACHING, PRE-RECORDED 
 * LESSONS, OFFLINE COURSES, BOOK WRITING, CONFERENCES, TRAININGS, PUBLIC WELFARE 
 * ACTIVITIES, PROJECTS, TOPICS, GRADUATION DESIGNS, DISSERTATIONS, THESES, AND 
 * OTHER APPLICATIONS.
 * 
 * THIS PROGRAM IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS PROGRAM, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>              
#include <unistd.h>             
#include "ohos_init.h"          
#include "cmsis_os2.h"          
#include "../global_settings.h" 
#include "sle_client.h"         
#if SSD1306_SUPPORT
#include "ssd1306.h" 
#endif
#if MQTT_SUPPORT
#include "wifi_connecter.h"     
#include "mqtt_task.h"          
#endif

#if MQTT_SUPPORT

#define PARAM_HOTSPOT_SSID "HUAWEI-Dragon"

#define PARAM_HOTSPOT_PSK "Good@123!"

#define PARAM_SERVER_ADDR "192.168.8.8"

#define PARAM_SERVER_PORT "1883"
#endif

static void ReceiverTask(char *arg)
{
    (void)arg;
    
#if MODE_DEBUG
    uapi_watchdog_disable();
#endif

#if MQTT_SUPPORT
    
    if (ConnectToHotspot(PARAM_HOTSPOT_SSID, PARAM_HOTSPOT_PSK) != 0)
    {
        
        printf("Connect to AP failed\n"); 
        return;
    }

    osDelay(100);

    MqttTaskInit();                                                     
    const char *host = PARAM_SERVER_ADDR;                               
    unsigned short port = atoi(PARAM_SERVER_PORT);                      
    const char *clientId = "DRSS00001";                                 
    const char *username = NULL;                                        
    const char *password = NULL;                                        
    if (MqttTaskConnect(host, port, clientId, username, password) != 0) 
    {
        
        printf("Connect to MQTT server failed!\r\n");
        return;
    }
    
    char *ptopic = "OpenHarmony/DRSS/DRSS00001/Message";                        
    char *payload = "{\"device_id\":\"DRSS00001\", \"status\":\"online\"}";     
    int rc = MqttTaskPublish(ptopic, payload);                                  
    if (rc != 0)                                                                
        printf("MQTT Publish failed!\r\n");
    else
        printf("MQTT Publish OK\r\n");
#endif

#if SSD1306_SUPPORT
    
    ssd1306_Init();

    ssd1306_Fill(Black);

    ssd1306_PrintString("Receiver node\r\n", Font_7x10, White);
#endif

    sle_set_server_name(RECEIVER_NODE_CHILD_ANNOUNCE_NAME);

    sle_client_init();
}

static void ReceiverDemo(void)
{
    
    osThreadAttr_t attr;
    attr.name = "ReceiverTask";
    attr.stack_size = 1024 * 60; 
    attr.priority = osPriorityNormal;

    if (osThreadNew(ReceiverTask, NULL, &attr) == NULL)
        printf("[ReceiverDemo] Falied to create ReceiverTask!\n");
    else
        printf("[ReceiverDemo] Create ReceiverTask successfully!\n");
}

APP_FEATURE_INIT(ReceiverDemo);
